
namespace Microsoft.Sdc.Tasks.BizTalk2004.ReceiveLocation
{
    #region Using directives

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;

    using Microsoft.Build.Framework;
    using Microsoft.Sdc.Tasks.BizTalk2004.Configuration;
    using Microsoft.Sdc.Tasks;

    #endregion


    public class Configure : BizTalk2004TaskBase
    {

        #region member variables

        private string name;
        private string address;
        private string publicAddress;
        private bool enable = true;
        private bool enableSpecified;

        private string receiveHandler;
        private string receivePipeline;
        private string receivePort;


        private bool endDateEnabled = false;
        private string endDate;


        private bool startDateEnabled = false;
        private string startDate;


        private string transportType;
        private string transportTypeData;


        private bool serviceWindowEnabled = false;
        private string startTime;
        private string endTime;

        #endregion


        #region properties

        /// <summary>
        /// Gets or sets the name of the receive location.
        /// </summary>
        /// <value>A string representing the name of this receive location.</value>
        [Required]
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        /// <summary>
        /// Gets the receive port that this receive location belongs to.
        /// </summary>
        [Required]
        public string ReceivePort
        {
            get { return this.receivePort; }
            set { this.receivePort = value; }
        }


        /// <summary>
        /// Gets or sets the address of the receive location.
        /// </summary>
        /// <value>A string representing the address of this receive location.</value>
        public string Address
        {
            get { return this.address; }
            set { this.address = value; }
        }

        /// <summary>
        /// Gets or sets the public address of the receive location.
        /// </summary>
        public string PublicAddress
        {

            get { return this.publicAddress; }
            set { this.publicAddress = value; }

        }

        /// <summary>
        /// Gets and sets the field to enable the receive location.
        /// An enabled receive location cannot be deleted.
        /// </summary>
        public bool Enable
        {
            get { return this.enable; }
            set { this.enable = value; }
        }

        /// <summary>
        /// Specifies if the enable parameter has been specified
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public bool EnableSpecified
        {
            get { return this.enableSpecified; }
            set { this.enableSpecified = value; }
        }

        /// <summary>
        /// Gets or sets the receive handler to use for this receive location.
        /// </summary>
        public string ReceiveHandler
        {
            get { return this.receiveHandler; }
            set { this.receiveHandler = value; }
        }

        /// <summary>
        /// Gets or sets the receive pipeline to use to receive messages at this receive location.
        /// </summary>
        public string ReceivePipeline
        {
            get { return this.receivePipeline; }
            set { this.receivePipeline = value; }
        }


        /// <summary>
        /// Gets and sets the transport type for this receive location.
        /// </summary>
        public string TransportType
        {
            get { return this.transportType; }
            set { this.transportType = value; }
        }

        /// <summary>
        /// Gets or sets the transport type properties of the receive location.
        /// </summary>
        public string TransportTypeData
        {
            get { return this.transportTypeData; }
            set { this.transportTypeData = value; }
        }

        /// <summary>
        /// Gets or sets the start date of the service window.
        /// </summary>
        public bool StartDateEnabled
        {
            get { return this.startDateEnabled; }
            set { this.startDateEnabled = value; }
        }

        /// <summary>
        /// Gets or sets the start date of the service window.
        /// </summary>
        public string StartDate
        {
            get { return this.startDate; }
            set { this.startDate = value; }
        }

        /// <summary>
        /// Gets or sets the field to enable the end date of the service window.
        /// </summary>
        public bool EndDateEnabled
        {
            get { return this.endDateEnabled; }
            set { this.endDateEnabled = value; }
        }

        /// <summary>
        /// Gets or sets the end date of the service window.
        /// </summary>
        public string EndDate
        {
            get { return this.endDate; }
            set { this.endDate = value; }
        }

        /// <summary>
        /// Gets the service window of this receive location.
        /// </summary>
        public bool ServiceWindowEnabled
        {
            get { return this.serviceWindowEnabled; }
            set { this.serviceWindowEnabled = value; }
        }

        /// <summary>
        /// Gets or sets the start time of the service window.
        /// </summary>
        public string StartTime
        {
            get { return this.startTime; }
            set { this.startTime = value; }
        }

        /// <summary>
        /// Gets or sets the end time of the service window.
        /// </summary>
        public string EndTime
        {
            get { return this.endTime; }
            set { this.endTime = value; }

        }

        #endregion


        public Configure() { }


        protected override void InternalExecute()
        {
            //loading
            Microsoft.Sdc.Tasks.BizTalk2004.Configuration.BizTalkInstallation installation = this.GetInstallation();
            Microsoft.Sdc.Tasks.BizTalk2004.Configuration.ReceivePort rxPort = Microsoft.Sdc.Tasks.BizTalk2004.Configuration.ReceivePort.Load(installation, this.receivePort);

            if (rxPort == null)
                throw new InvalidOperationException("Receive Port does not exist.");


            Microsoft.Sdc.Tasks.BizTalk2004.Configuration.ReceiveLocation rxLocation = rxPort.ReceiveLocations[this.name];

            if (rxLocation == null)
                throw new InvalidOperationException("The Receive Location does not exist.");

            if (this.receiveHandler != null && (this.transportType == null || this.transportTypeData == null))
            {
                throw new InvalidOperationException("If the ReceiveHandler is specficied, the TransportType and TransportTypeData must also be specified");
            }

            //setting the properties
            if (!string.IsNullOrEmpty(this.address))
            {
                rxLocation.Address = this.address;
            }
            if (!string.IsNullOrEmpty(this.publicAddress))
            {
                rxLocation.PublicAddress = this.publicAddress;
            }

            if (this.enableSpecified)
            {
                rxLocation.Enable = this.enable;
            }

            if (!string.IsNullOrEmpty(this.receiveHandler))
            {
                rxLocation.ReceiveHandler = Microsoft.Sdc.Tasks.BizTalk2004.Configuration.ReceiveHandler.Load(installation, this.receiveHandler, this.transportType);
            }

            if (!string.IsNullOrEmpty(this.receivePipeline))
            {
                string _pipelineName;
                string _assemblyName;
                SplitQualifiedTypeName(this.receivePipeline, out _pipelineName, out _assemblyName);

                Microsoft.Sdc.Tasks.BizTalk2004.Configuration.BizTalkAssembly _assembly = Microsoft.Sdc.Tasks.BizTalk2004.Configuration.BizTalkAssembly.Load(installation, _assemblyName);
                rxLocation.ReceivePipeline = _assembly.Pipelines[_pipelineName];
            }

            rxLocation.StartDateEnabled = this.startDateEnabled;
            rxLocation.StartDate = (!string.IsNullOrEmpty(this.startDate)) ? XmlConvert.ToDateTime(this.startDate, XmlDateTimeSerializationMode.Utc) : rxLocation.StartDate;

            rxLocation.EndDateEnabled = this.endDateEnabled;
            rxLocation.EndDate = (!string.IsNullOrEmpty(this.endDate)) ? XmlConvert.ToDateTime(this.endDate, XmlDateTimeSerializationMode.Utc) : rxLocation.EndDate;

            if (!string.IsNullOrEmpty(this.transportType))
            {
                rxLocation.TransportType = Microsoft.Sdc.Tasks.BizTalk2004.Configuration.Protocol.Load(installation, this.transportType);
                rxLocation.TransportTypeData = new Microsoft.Sdc.Tasks.BizTalk2004.Configuration.TransportData(this.transportTypeData);
            }

            if (this.serviceWindowEnabled)
                rxLocation.ServiceWindow = new Microsoft.Sdc.Tasks.BizTalk2004.Configuration.ServiceWindow(true, XmlConvert.ToDateTime(this.startTime, XmlDateTimeSerializationMode.Utc), XmlConvert.ToDateTime(this.endTime, XmlDateTimeSerializationMode.Utc));
            else
                rxLocation.ServiceWindow = new Microsoft.Sdc.Tasks.BizTalk2004.Configuration.ServiceWindow(false);

            //saving
            rxLocation.Save();
        }
    }
}

